package com.kingdee.eas.cp.bc.loan;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;

import org.springframework.ui.ModelMap;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.dao.query.SQLExecutorFactory;
import com.kingdee.bos.eventbus.event.action.KDActionEvent;
import com.kingdee.bos.webframework.exception.WafException;
import com.kingdee.bos.webframework.dynamic.application.WebSymbolConstant;
import com.kingdee.bos.webframework.dynamic.handler.AbstractWebEventHandler;
import com.kingdee.bos.eventbus.event.action.IActionEventHandler;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.eas.cp.bc.DailyLoanBillCollection;
import com.kingdee.eas.cp.bc.DailyLoanBillFactory;
import com.kingdee.eas.cp.bc.DailyLoanBillInfo;
import com.kingdee.eas.cp.bc.StateEnum;
import com.kingdee.eas.custom.cost.BorrowingType;
import com.kingdee.jdbc.rowset.IRowSet;

public class SubmitActionEvent extends AbstractWebEventHandler implements IActionEventHandler {
	public void onAction(KDActionEvent event) throws WafException,BOSException {
		ModelMap modelMap = event.getReqeustContext().get(
				ModelMap.class);
		DailyLoanBillInfo model = (DailyLoanBillInfo) modelMap.get(WebSymbolConstant.MODEL_ATTRIBUTE_KEY);
		String borrowingType = model.getString("borrowingType");
		if(borrowingType.equals("5")){
			Set set=new HashSet();
			set.add(StateEnum.DRAFT_VALUE);
			set.add(StateEnum.ALREADYABANDON_VALUE);
			set.add(StateEnum.CHECKFAILD_VALUE);
			EntityViewInfo view=new EntityViewInfo();
			FilterInfo filter=new FilterInfo();
			filter.getFilterItems().add(new FilterItemInfo("state",set,CompareType.NOTINCLUDE));
			filter.getFilterItems().add(new FilterItemInfo("applier.id",model.getApplier().getId()));
//			filter.getFilterItems().add(new FilterItemInfo("AmountBalance",0,CompareType.GREATER));
//			if(model.getState().equals(StateEnum.SUBMIT_VALUE)){
				filter.getFilterItems().add(new FilterItemInfo("id",model.getId(),CompareType.NOTEQUALS));	
//			}
			
			filter.getFilterItems().add(new FilterItemInfo("borrowingType",5));
			view.setFilter(filter);
			DailyLoanBillCollection col = DailyLoanBillFactory.getRemoteInstance().getDailyLoanBillCollection(view);
			for(int i=0;i<col.size();i++){
				DailyLoanBillInfo info = col.get(i);
				BigDecimal amountApproved=new BigDecimal(0);
				if(null!=info.getAmountApproved()){
					amountApproved=info.getAmountApproved();
				}
				BigDecimal repaymentAmount=new BigDecimal(0);
				if(null!=info.getReturnAmount()){
					repaymentAmount=info.getReturnAmount();
				}
				if(null!=info.getAmountUsed()){
					repaymentAmount=repaymentAmount.add(info.getAmountUsed());
				}
				if(amountApproved.compareTo(repaymentAmount)!=0){
					throw new WafException("上一笔职工借支借款未还清,不得再借第二笔!");
				}
			}
			
		}else if(borrowingType.equals("1")||borrowingType.equals("2")||borrowingType.equals("3")||borrowingType.equals("4")){
			if(null==model.get("contractMoney")){
				throw new WafException("合同总额不能为空!");
			}
			
			if(null==model.get("contractNumber")){
				throw new WafException("合同编号不能为空!");
			}
			
			if(null==model.get("paid")){
				throw new WafException("已付款不能为空!");
			}
		}
		
		String[] splits=model.getNumber().split("-");
		String name=BorrowingType.getEnum(borrowingType).getName();
		if(splits.length==2){
			model.setNumber(splits[0]+"-"+name+"-"+splits[1]);
		}else if(splits.length==3){
			model.setNumber(splits[0]+"-"+name+"-"+splits[2]);
		}
	}
}